/*
This file produces a tables of descriptives based on the combined CQI/VVIV/SA302 data
*/


*************************************************************************************
* Table 1: proportion of individuals with positive amounts of each income component *
*************************************************************************************

capture program drop tableOfPropWithEachIncComponent
program define tableOfPropWithEachIncComponent

	preserve

	syntax [using/]
	if ("`using'" != "") use "`using'", clear

	
	local incomeComponents "empinc_cpi sempinc_cpi intinc_cpi divinc_cpi pensinc_cpi sharesinc_cpi propinc_cpi foreigninc_cpi trustestatinc_cpi othinc_cpi"

	* intinc_cpi and divinc_cpi are missing in 1999, so we want to make sure 1999 is excluded for these components
	* Previous cleaning has set them equal to zero for non-survivors
	* We want to set these back to missing in 1999
	qui replace intinc_cpi = . if (tax_year == 1999)
	qui replace divinc_cpi = . if (tax_year == 1999)

	* Keep only control observations, since these are a random sample and aren't affected by audit
	qui keep if (treatment == 0)
	
	di as text %22s "" %18s "Mean" %18s "N"
	foreach var of varlist `incomeComponents' {
		
		qui gen byte getsincome = (`var' > 0) if (`var' < .)
		su getsincome, meanonly
		scalar count = r(N)
		scalar mean = r(mean)
		di as text %22s "`var'" %18.3f scalar(mean) %18.0f scalar(count)  
		scalar drop mean count 
		drop getsincome 
	}
	
	restore

end





************************************************
* Figure A1: random audit outcomes by subgroup *
************************************************

capture program drop tableOfAuditSubgroupOutcomes
program define tableOfAuditSubgroupOutcomes

	preserve

	syntax [using/]
	if ("`using'" != "") use "`using'", clear
	
	* Create some variables
	***********************
	
	* Quintile of declared income in year -1
	qui fasterxtile incquintile = taxableinccg_cpi if ((yrssince == -1) & (taxableinccg_cpi > 0 & taxableinccg_cpi < .)), nquantiles(5)
	qui replace incquintile = 0 if (incquintile == . & yrssince == -1)
	gegen byte maxincquintile = max(incquintile), by(utr_no audityear)
	drop incquintile
	rename maxincquintile incquintile
	label variable incquintile "Income quintile in yrssince=-1"
	label values incquintile incquintile
	
	* Drop all observations except in yrssince=0
	keep if (yrssince == 0)


	* Print table
	*************

	* Keep settled random audits
	keep if (random_sig == "Y") & (status_ind == "S")
	keep if (direct_dutyenq_amt_cpi < .)

	* Identify the noncompliers
	gen byte yieldpositive = inlist(stlmnt_class_ind,"C","1","2","3")
	
	* Calculate proportional increment to taxes
	qui gen double direct_dutyenq_amt_pcent = direct_dutyenq_amt_cpi / (it_cl4_cgt_cpi + direct_dutyenq_amt_cpi) 
		
	forvalues fake = 1/1 { //to make it print nicely
	
		di as text %20s "Group" %20s "Mean tax" %20s "Tax se" %20s "Share noncom" %20s "Share noncom se" %20s "Mean if noncom" %20s "Mean if noncom se" %20s "Mean pc if noncom" %20s "Mean pc if noncom se" %20s "N" %20s "Share in population"
		
		* Overall
		qui su it_cl4_cgt_cpi 
		scalar inctaxmean = r(mean)
		scalar inctaxsd = r(sd)/sqrt(r(N))
		qui su yieldpositive
		scalar sharenoncom = r(mean)
		scalar sharenoncomsd = r(sd)/sqrt(r(N))
		scalar N = r(N)
		qui su direct_dutyenq_amt_cpi if yieldpositive
		scalar meanifnoncom = r(mean)
		scalar meanifnoncomsd = r(sd)/sqrt(r(N))
		qui su direct_dutyenq_amt_pcent if yieldpositive
		di as text %20s "Overall" %20.3f scalar(inctaxmean) %20.3f scalar(inctaxsd) %20.3f scalar(sharenoncom) %20.3f scalar(sharenoncomsd) %20.0f scalar(meanifnoncom) %20.0f scalar(meanifnoncomsd) %20.3f r(mean) %20.3f r(sd)/sqrt(r(N))  %20.0f scalar(N)  %20.3f 1.000
		

		* Quintile of declared income in year -1
		forval i = 0/5 {
			qui gen byte thisgroup = (incquintile == `i')  if (incquintile < .) 
			qui su thisgroup
			scalar shareinpop = r(mean)
			qui su it_cl4_cgt_cpi if thisgroup 
			scalar inctaxmean = r(mean)
			scalar inctaxsd = r(sd)/sqrt(r(N))
			qui su yieldpositive if thisgroup 
			scalar sharenoncom = r(mean)
			scalar sharenoncomsd = r(sd)/sqrt(r(N))
			scalar N = r(N)
			qui su direct_dutyenq_amt_cpi if yieldpositive & thisgroup 
			scalar meanifnoncom = r(mean)
			scalar meanifnoncomsd = r(sd)/sqrt(r(N))
			qui su direct_dutyenq_amt_pcent if yieldpositive & thisgroup 
			di as text %20s "Quintile`i'" %20.3f scalar(inctaxmean) %20.3f scalar(inctaxsd) %20.3f scalar(sharenoncom) %20.3f scalar(sharenoncomsd) %20.0f scalar(meanifnoncom) %20.0f scalar(meanifnoncomsd) %20.3f r(mean) %20.3f r(sd)/sqrt(r(N)) %20.0f scalar(N) %20.3f scalar(shareinpop)
			drop thisgroup
		}

	}

	restore
	
end 


*******************************
* Table 4: income persistence *
*******************************

capture program drop persistanceOfIncomeSource
program define persistanceOfIncomeSource

	preserve

	syntax [using/]
	if ("`using'" != "") use "`using'", clear

	*only want to use controls or pre-treatment treated obs
	keep if yrssince<0 | treatment==0
	egen id = group(utr_no audityear)
	xtset id yrssince

	*just makes it display nicely	
	forvalues fake = 1/1 {
	
		di as text %20s "Inc Source" %20s "Autocorr(t,t-1)" %20s "Autocorr(t,t-2)" %20s "Autocorr(t,t-3)" %14s "N" 
	
		foreach var of varlist pensinc_cpi propinc_cpi empinc_cpi intinc_cpi sempinc_cpi divinc_cpi  {
			qui xtsum `var' 
			scalar N = r(N)
				forvalues i=1/5 {
				qui corr `var' L`i'.`var' 
				scalar autocorr_l`i'  =  r(rho)
			}
			di as text %20s "`var'"    %20.3f scalar(autocorr_l1)  %20.3f scalar(autocorr_l2)  %20.3f scalar(autocorr_l3)  %14.0f scalar(N)
		}
	
	}
	
end


exit
